package com.customview.custmoCircle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.customview.R;


/**
 * Created by Administrator on 2017/2/22.
 */
public class CirleView extends View {
    private  Paint paint;
    int defaultR=0;



    public CirleView(Context context) {
        this(context, null);
    }

    public CirleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CirleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCircle, defStyleAttr, 0);

        //获取多少个自定义属性
        int indexCount = typedArray.getIndexCount();

        // 初始化  这些属性问题

        //.......
        for (int i = 0; i <indexCount ; i++) {

            int index = typedArray.getIndex(i);

            switch (index)
            {
                case R.styleable.CustomCircle_mySize:

                    // 默认设置为16sp，TypeValue也可以把sp转化为px
                    defaultR = typedArray.getDimensionPixelSize(index, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 80, getResources().getDisplayMetrics()));
                    Log.d("ddd","defaultR:"+defaultR);
                    break;

            }
        }

        //回收
        typedArray.recycle();

        //画笔
       paint = new Paint();
       paint.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //测量 整个空间  大小
        int width=0;
        int height=0;
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);

        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);


        switch (widthmode)
        {
            //wrap_content
            case MeasureSpec.AT_MOST:

                width=getPaddingLeft()+defaultR+getPaddingRight();
                break;
            case MeasureSpec.EXACTLY:
                width=getPaddingLeft()+widthsize+getPaddingRight();
            break;

        }
        switch (heightmode) {
            //wrap_content
            case MeasureSpec.AT_MOST:
                height = getPaddingLeft() + defaultR + getPaddingRight();
                break;
            case MeasureSpec.EXACTLY:
                height = getPaddingLeft() + heightsize + getPaddingRight();
                break;



        }
        setMeasuredDimension(width, height);



    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.RED);
        // 坐标
        int cx=getHeight()/2;
        int cy=getWidth()/2;


        Log.d("自定义控件", "onDraw: cx"+cx+"\n"
        +"cy"+cy
        );
        //最大路径不能  超过  最短的边长
        int temp_width =(getWidth()-getPaddingLeft()-getPaddingRight())/2;
        int temp_heigth=(getHeight()-getPaddingTop()-getPaddingBottom())/2;
        int tmpR=Math.min(temp_width,temp_heigth);
        defaultR=Math.min(defaultR,tmpR);
        canvas.drawCircle(cx, cy, defaultR, paint);

    }
}
